﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EffortsTracker
{
    public partial class ProjectsView : UserControl
    {
        TrackerView trackerView;
        Project[] projects = null;

        public ProjectsView(TrackerView trackerView)
        {
            this.trackerView = trackerView;
            InitializeComponent();
            trackerView.Height = this.Height + 40;
            trackerView.Width = this.Width + 20;
        }

        private void ProjectsView_Load(object sender, EventArgs e)
        {
            initializeView();
        }

        private void initializeView()
        {
            projects = Project.getAllFullProjects();
            projectsDataGrid.Rows.Clear();
            for (int i = 0; i < projects.Length; i++)
            {
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell.Value = projects[i].name;
                row.Cells.Add(dataGridViewTextBoxCell);
                dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell.Value = projects[i].initialStartDate.ToString(Database.dateFormat);
                row.Cells.Add(dataGridViewTextBoxCell);
                dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell.Value = projects[i].finalEndDate.ToString(Database.dateFormat);
                row.Cells.Add(dataGridViewTextBoxCell);
                dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell.Value = projects[i].getFormattedHours();
                row.Cells.Add(dataGridViewTextBoxCell);
                projectsDataGrid.Rows.Add(row);
            }
        }

        private void resumeButton_Click(object sender, EventArgs e)
        {
            if (projects != null && projectsDataGrid.SelectedRows[0].Index < projects.Length)
            {
                trackerView.panel1.Controls.Clear();
                trackerView.panel1.Controls.Add(new TrackerUserControl(trackerView, projects[projectsDataGrid.SelectedRows[0].Index]));
            }
        }

        private void newProjectButton_Click(object sender, EventArgs e)
        {
            trackerView.panel1.Controls.Clear();
            trackerView.panel1.Controls.Add(new AddProjectView(trackerView));
        }

        private void projectsDataGrid_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (projects != null && e.RowIndex < projects.Length)
            {
                trackerView.panel1.Controls.Clear();
                trackerView.panel1.Controls.Add(new TrackerUserControl(trackerView, projects[e.RowIndex]));
            }
        }

        private void projectsDataGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (projects != null && projectsDataGrid.SelectedRows[0].Index < projects.Length)
                {
                    trackerView.panel1.Controls.Clear();
                    trackerView.panel1.Controls.Add(new TrackerUserControl(trackerView, projects[projectsDataGrid.SelectedRows[0].Index]));
                }
            }
        }

        private void exportButton_Click(object sender, EventArgs e)
        {
            if (projects != null && projectsDataGrid.SelectedRows[0].Index < projects.Length)
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    //MessageBox.Show(folderBrowserDialog.SelectedPath);
                    projects[projectsDataGrid.SelectedRows[0].Index].export(folderBrowserDialog.SelectedPath);
                }
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (projects != null && projectsDataGrid.SelectedRows[0].Index < projects.Length)
            {
                projects[projectsDataGrid.SelectedRows[0].Index].delete();
                initializeView();
            }
        }
    }
}